package cn.sdormitory.controller.report;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.report.service.ClassAttenceStaService;
import cn.sdormitory.report.service.ClassHygieneStaService;
import cn.sdormitory.report.vo.ClassAttenceStaVO;
import cn.sdormitory.report.vo.ClassHygieneStaVO;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @创建人：zhouyang
 * @创建时间：2022/1/19 16:31
 * @version：V2.0
 */
@RestController
@Api(tags = "Report-ClassHygieneStatistics=> 班级卫生统计")
@RequestMapping("/report/classhygienestatistics")
public class ClassHygieneStaController {
    @Autowired
    private ClassHygieneStaService classHygieneStaService;

    @ApiOperation("list => 查询某月班级卫生统计列表")
    @PreAuthorize("@ss.hasPermi('report:classhygienestatistics:list')")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<ClassHygieneStaVO>> list(@RequestParam Map<String, Object> params) {
        String yearMonth=(String)params.get("inYearMonth");
        String classDeptId=(String)params.get("classDeptId");
        String inYearMonth="";
        if("".equals(yearMonth) || yearMonth==null){
            Date date = new Date();
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
            String date1=fmt.format(date);
            inYearMonth=date1;
        }else{
            inYearMonth=yearMonth;
        }
        List<ClassHygieneStaVO> hygieneStatisticsVOList = classHygieneStaService.getClassHygieneStaList(inYearMonth,classDeptId,Integer.parseInt((String)params.get("pageNum")),Integer.parseInt((String)params.get("pageSize")));
        if(hygieneStatisticsVOList!=null && hygieneStatisticsVOList.size()>0){
            for (ClassHygieneStaVO hygieneStatisticsVO:hygieneStatisticsVOList){
                hygieneStatisticsVO.setInYearMonth(inYearMonth);
            }
        }
        CommonPage<ClassHygieneStaVO> commonPage = new CommonPage<ClassHygieneStaVO>();
        commonPage.setList(hygieneStatisticsVOList);
        Long count=classHygieneStaService.getClassHygieneStaListCount(inYearMonth,classDeptId);
        commonPage.setTotal(count);
        return CommonResult.success(commonPage);

    }

    @ApiOperation("hygienelist =>导出班级卫生统计信息")
    @PreAuthorize("@ss.hasPermi('report:classhygienestatistics:classhygienelist')")
    @GetMapping(value = "/exportclasshygienelist")
    public CommonResult export(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        String yearMonth=(String)params.get("inYearMonth");
        String classDeptId=(String)params.get("classDeptId");
        String inYearMonth="";
        if("".equals(yearMonth) || yearMonth==null){
            Date date = new Date();
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
            String date1=fmt.format(date);
            inYearMonth=date1;
        }else{
            inYearMonth=yearMonth;
        }
        List<ClassHygieneStaVO> hygieneStatisticsVOList = classHygieneStaService.getClassHygieneStaListExport(inYearMonth,classDeptId);
        try {
            EasyExcel.write(response.getOutputStream(), ClassHygieneStaVO.class).sheet("班级卫生统计管理").doWrite(hygieneStatisticsVOList);
            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }
}
